<template>
  <div class="main">
    <h1>电商后台管理系统</h1>
    <div class="login_box">
      <div class="avatar_box">
        <img src="./../../assets/logo.png"/>
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username" >
          <el-input v-model="loginForm.username" placeholder="请输入用户名" clearable>
            <i slot="prefix" class="el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" clearable>
            <i slot="prefix" class="el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <template slot-scope="">
            <el-button type="primary" class="login_btn" @click="login">登录</el-button>
            <el-button class="login_btn" @click="loginForm = {}">重置</el-button>
          </template>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user/user'
export default {
  name: 'login',
  data: () => {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      input: '',
      loginFormRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await login(this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        window.sessionStorage.setItem('user', JSON.stringify(res.data))
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang="less">
  @import url("./index.less");
</style>
